var users = [];
var currentPage = 1;
var totalPage = 1;
// 查询所有用户信息
function queryAllUsers() {
	var options = {
		"accountNumber" : localStorage.accountNum,
		"token" : localStorage.token
	};
	$.ajax({
		type : "POST",
		url : "/ThreeNewBoard/queryalluser",
		data : JSON.stringify(options),
		crossDomain : true,
		async : true,
		contentType : "application/json",
		dataType : "json",
		success : function(result) {
			if (result.message == "SUCCESS") {
				// data为请求到的数据
				var data = result.data;
				users = data;
				totalPage = Math.ceil(data.length / 10);
				if (totalPage === 0) {
					var noData = "<div style='width:100%;height:30px;margin:40px 0;text-align: center;'>暂无表格数据</div>"
					document.getElementById("userListNoData").innerHTML = noData;
				}
				document.getElementById("userTotal").innerText = "";
				$("#userTotal").append(totalPage);
				dealData();
			} else {
				alert("数据请求错误！");
			}
		}
	});
			
}
// 审核
function approval(row, index) {
	var item = row.parentElement.children[index];
	var options = {
		"accountNumber" : localStorage.accountNum,
		"token" : localStorage.token,
		"amountAccountNumber" : item.cells[1].innerText
	};
	$.ajax({
		type : "POST",
		url : "/ThreeNewBoard/accountReview",
		data : JSON.stringify(options),
		crossDomain : true,
		async : true,
		contentType : "application/json",
		dataType : "json",
		success : function(result) {
			var data = result.data;
			if (data.status) {
				// data为请求到的数据
				alert(data.promptMessage);
				queryAllUsers();
			} else {
				alert("数据请求错误！");
			}
		}
	});
}
// 修改密码
function modifyPwd(row, index) {
	var newPsd = document.getElementById("newPsd").value;
	var againNewPsd = document.getElementById("againNewPsd").value
	var newPsdError = document.getElementById("newPsdMsg").innerHTML;
	var againNewPsdError = document.getElementById("againNewPsdMsg").innerHTML;
	if(newPsd === ""){
		alert("请输入新密码。");
		return;
	}
	if(againNewPsd === ""){
		alert("请再次确认密码。");
		return;
	}
	if (newPsdError) {
		alert("密码不合法，请确认。");
		return;
	}
	if (againNewPsdError) {
		alert("两次输入密码不一致，请确认。");
		return;
	}
	closeWindow('modifyUserPsdModal');
	var item = row.parentElement.children[index];
	var options = {
		"accountNumber" : localStorage.accountNum,
		"token" : localStorage.token,
		"newPassWord" : document.getElementById("newPsd").value,
		"number" : item.cells[1].innerText
	};
	$.ajax({
		type : "POST",
		url : "/ThreeNewBoard/adminchpasswd",
		data : JSON.stringify(options),
		crossDomain : true,
		async : true,
		contentType : "application/json",
		dataType : "json",
		success : function(result) {
			var data = result.data;
			if (data.status) {
				// data为请求到的数据
				alert(data.promptMessage);
				queryAllUsers();
			} else {
				alert("数据请求错误！");
			}
		}
	});
}
// 修改密码弹窗
function modifyPsdWin(row, index) {
	modifyIndex = index;
	document.getElementById("newPsd").value = "";
	openWindow('modifyUserPsdModal');
}
//校验密码
function validNewPsd(value,id){
	document.getElementById(id).innerText = "";
	if (!passwordValidator.test(value)) {
		document.getElementById(id).innerText = "密码以字母开头，长度在6-18之间，只能包含英文字母、数字和下划线。";
		return false;
	}
}
//确认密码
function confirmPsd(value,id){
	document.getElementById(id).innerText = "";
	var psd = document.getElementById("newPsd").value;
	if (value !== psd) {
		document.getElementById(id).innerText = "两次输入密码不一致。";
		return false;
	}
}
// 处理表格数据展示
function dealData() {
	var displayData = [];
	$("#userList").find("tr").remove();
	if (totalPage < 1 || currentPage === totalPage) {
		$("#nextPageBtn").attr("disabled", true);
	} else {
		$("#nextPageBtn").attr("disabled", false);
	}
	if (currentPage === 1) {
		$("#prePageBtn").attr("disabled", true);
	} else {
		$("#prePageBtn").attr("disabled", false);
	}
	document.getElementById("currentPage").innerText = currentPage;
	var j=0;
	for (var i = (currentPage - 1) * 10; i < currentPage * 10; i++) {
		var item = users[i];
		if (!item) {
			return;
		}
		item.index = j;
		if (item.accountStatus === "等待审核") {
			item.operateLink = "<a class='commonLink' style='color:#00f' onclick='approval(row,"
					+ item.index + ")'>资料审核</a>"
		} else {
			item.operateLink = "<a class='commonLink' style='color:#00f' onclick='modifyPsdWin(row,"
					+ item.index + ")'>修改密码</a>"
		}
		displayData.push(item);
		row = document.createElement("tr"); // 创建行
		td1 = document.createElement("td"); // 姓名
		td2 = document.createElement("td"); // 账号
		td3 = document.createElement("td"); // 身份证号
		td4 = document.createElement("td"); // 手机号
		td5 = document.createElement("td"); // 余额
		td6 = document.createElement("td"); // 审核状态
		td7 = document.createElement("td"); // 操作列
		td1.appendChild(document.createTextNode(item.userName)); // 为单元格添加内容 
		td2.appendChild(document.createTextNode(item.accountNumber));
		td3.appendChild(document.createTextNode(item.idCard));
		td4.appendChild(document.createTextNode(item.phone));
		td5.appendChild(document.createTextNode(item.balance));
		td6.appendChild(document.createTextNode(item.accountStatus));
		td7.innerHTML = item.operateLink;
		row.appendChild(td1);
		row.appendChild(td2);
		row.appendChild(td3);
		row.appendChild(td4);
		row.appendChild(td5);
		row.appendChild(td6);
		row.appendChild(td7);
		$("#userList").append(row);
		j++;
	}
}
// 上一页
function prePage() {
	currentPage--;
	dealData();
}
// 下一页
function nextPage() {
	currentPage++;
	dealData();
}
